package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.utils.DateUtil;
import qc.module.ehs.dto.certificate.CertificateDto;
import qc.module.ehs.entity.EhsCertificateInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * EhsCertificateInfoMapper
 * @author YRR
 * @create 2025-03-11 14:55
 */
@Mapper
public interface EhsCertificateInfoMapper {
    EhsCertificateInfoMapper MAPPER = Mappers.getMapper(EhsCertificateInfoMapper.class);

    @Mapping(target = "assigntm", source = "assigntm", dateFormat = "yyyy-MM-dd")
    @Mapping(target = "expiredtm", source = "expiredtm", dateFormat = "yyyy-MM-dd")
    @Mapping(target = "isvalid", ignore = true)
    CertificateDto toDto(EhsCertificateInfo en);

    default CertificateDto entityToDto(EhsCertificateInfo en) {
        CertificateDto dto = EhsCertificateInfoMapper.MAPPER.toDto(en);
        if (en.getExpiredtm() == null) {
            dto.setIsvalid(true);
        } else {
            boolean isValid = en.getExpiredtm().after(DateUtil.getNowDate());
            dto.setIsvalid(isValid);
        }
        return dto;
    }

    default List<CertificateDto> toDtoList(List<EhsCertificateInfo> enList) {
        List<CertificateDto> list = new ArrayList<>();
        for (EhsCertificateInfo en : enList) {
            list.add(entityToDto(en));
        }
        return list;
    }

    @Mapping(target = "assigntm", ignore = true)
    @Mapping(target = "expiredtm", ignore = true)
    EhsCertificateInfo toEntity(CertificateDto dto);
}
